package com.avcdata.national.adapter;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.national.module.AreaListViewInfo;
import com.avcdata.national.utils.MyUtils;

import java.util.List;

/**
 * NewAOSApp
 * com.avcdata.national.adapter
 * Created by lic on 2017/9/21.
 * CSDN http://blog.csdn.net/lucasey
 */

public class AreaAdapter extends BaseAdapter {

    private List<AreaListViewInfo> list;
    private Context context;

    public AreaAdapter(List<AreaListViewInfo> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_area, null);
            holder = new viewHolder();
            holder.bigRegin = (TextView) convertView.findViewById(R.id.bigRegin);
            holder.marketShare = (TextView) convertView.findViewById(R.id.market_share);
            holder.basicRatio = (TextView) convertView.findViewById(R.id.basicRatio);
            holder.realRatio = (TextView) convertView.findViewById(R.id.RelaRation);
            holder.tongbiIcon = (ImageView) convertView.findViewById(R.id.tongbi_icon);
            holder.huanbiIcon = (ImageView) convertView.findViewById(R.id.huanbi_icon);
            convertView.setTag(holder);
        }else{
            holder = (viewHolder) convertView.getTag();
        }

        holder.bigRegin.setText(list.get(position).getBigRegin());
        holder.marketShare.setText(list.get(position).getMarketShare());

        try{
            holder.marketShare.setText(MyUtils.format2(Math.abs(Double.parseDouble(list.get(position).getMarketShare())))+"%");
        }catch (Exception e){
            e.printStackTrace();
            holder.marketShare.setText("--");
        }

        try{
            holder.basicRatio.setText(MyUtils.format2(Math.abs(Double.parseDouble(list.get(position).getBasicRatio())))+"%");
            if(Double.parseDouble(list.get(position).getBasicRatio())<0){
                holder.basicRatio.setTextColor(context.getResources().getColor(R.color.green));
                holder.tongbiIcon.setVisibility(View.GONE);
            }else{
                holder.basicRatio.setTextColor(context.getResources().getColor(R.color.red));
                holder.tongbiIcon.setVisibility(View.GONE);
            }
        }catch (Exception e){
            holder.basicRatio.setText("--");
            holder.basicRatio.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.tongbiIcon.setVisibility(View.GONE);
        }

        try{
            holder.realRatio.setText(MyUtils.format2(Math.abs(Double.parseDouble(list.get(position).getRelaRation())))+"%");
            if(Double.parseDouble(list.get(position).getRelaRation())<0){
                holder.realRatio.setTextColor(context.getResources().getColor(R.color.green));
                holder.huanbiIcon.setVisibility(View.GONE);
            }else{
                holder.realRatio.setTextColor(context.getResources().getColor(R.color.red));
                holder.huanbiIcon.setVisibility(View.GONE);
            }
        }catch (Exception e){
            holder.realRatio.setText("--");
            holder.realRatio.setTextColor(context.getResources().getColor(R.color.text_one));
            holder.huanbiIcon.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class viewHolder{
        private TextView bigRegin;
        private TextView marketShare;
        private TextView basicRatio;
        private TextView realRatio;
        private ImageView tongbiIcon;
        private ImageView huanbiIcon;
    }

}
